<?php

declare(strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;
use app\common\controller\Config;
use think\facade\Db;

class DealRelease extends Base
{
    protected $name = 'deal_release';
    /**
     * 待出售积分释放
     */
    public static function waitRelease()
    {
        try {
            $list = Assets::where('wait', '>', 0)->field('uid,wait')->select();
            if (!$list->isEmpty()) {
                foreach ($list as &$info) {
                    Target::Account()->basics($info->uid, 'wait', $info->wait, '待出售积分释放', '-')->createNormal();
                    Target::Account()->basics($info->uid, 'sold', $info->wait, '待出售积分释放', '+')->createNormal();
                }
            }
            $time = date('Y-m-d',(time()- 86400*3));
            $entime = strtotime(date('Y-m-d'));
            $rlist = Db::name('deal_release')->whereDay('create_time',$time)->where('status',0)->field('id,uid,money,status')->select();
            if (!$rlist->isEmpty()) {
                foreach ($rlist as &$v) {
                    parent::where('id',$v['id'])->update(['status'=>1]);
                    $user = User::where(['id' => $v['uid']])->field('id')->find();
                    $money = $v['money'];
                    if($v['money']>$user->assets->lock){
                        $money = $user->assets->lock;
                    }
                    $sum = Db::name('user_account')->where([['create_time','>=',strtotime($time)],['create_time','<=',$entime]])->where('uid',$v['uid'])->where(['intro'=>'交易完成锁仓释放','fx'=>'-','type'=>'lock'])->sum('amount');
                    if($sum>0){
                        if($sum>$money){
                            $money = 0;
                        }else{
                            $money = round($money-$sum,2);
                        }
                        if($money>0){
                            Target::Account()->basics($v['uid'], 'lock', $money, '锁仓积分销毁', '-')->createNormal();
                        }
                    }else{
                        if($money>0){
                            Target::Account()->basics($v['uid'], 'lock', $money, '锁仓积分销毁', '-')->createNormal();
                        }
                    }
                }
            }
            return ['code' => 1, 'msg' => '释放成功！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 回退释放
     */
    public static function return_lock()
    {
        try {
           
            $time = date('Y-m-d',(time()- 86400*3));
            $rlist = parent::whereDay('create_time',$time)->where('status',1)->field('id,uid,money,status')->select();
            // print_r($rlist);die;
            if (!$rlist->isEmpty()) {
                foreach ($rlist as &$v) {
                    parent::where('id',$v->id)->update(['status'=>0]);
                    // Target::Account()->basics($v->uid, 'lock', $v->money, '锁仓积分返还', '+')->createNormal();
                }
            }
            return ['code' => 1, 'msg' => '释放成功！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
